<?php
namespace app\admin\controller;

use app\agent\model\AgentOrder;
use think\Request;
use app\lib\exception\LoginException;
use app\lib\exception\SuccessMessage;
use app\agent\model\Agent;

class Order extends Base {
    public function index() {
        $merId = Request::instance()->param('merId');
        $beginTransDate = Request::instance()->param('beginTransDate',date("Ymd",strtotime("-1 day")));
        $endTransDate = Request::instance()->param('beginTransDate',date("Ymd",strtotime("-1 day")));
        $arr = array();
        $arr['reqType'] = 'payList';
        $arr['merId'] = $merId;
        $arr['beginTransDate'] = 20171101;
        $arr['endTransDate'] = 20171124;
        $arr['transSeqId'] = '';
        $arr['transStat'] = '';
        $arr['pageNum'] = '1';
        $arr['pageSize'] = '100';
        $url = 'http://app.ky365.cn/mobile/entry.do';
        $passStr = 'ky36588888';
        $str = $arr['reqType'].$arr['merId'].$arr['beginTransDate']. $arr['endTransDate'].$arr['pageNum'].$arr['pageSize'].$passStr ;
        $arr['chkValue'] = md5($str);
        $resdata = curl_form($url,$arr);
        $list = json_decode(mb_convert_encoding($resdata,'UTF-8','GBK'),true);
        p($list);
        //$this->assign('list', $list);
        //return view();
    }

    //订单详情
    public function detail() {
        $id = Request::instance()->param('id');

        $this->assign('list', AgentOrder::get($id));
        return view();
    }

    //与下级代理商的订单
    public function width_agent() {
        if(Request::instance()->post()) {
            $name = Request::instance()->post('agent_name') ? Request::instance()->post('agent_name') : null;

            $searchData = array(
                'b.name' => array('like', "%".$name."%"),
                'a.status' => 1,
            );

            $list = AgentOrder::name('agent_order')->alias('a')
                ->join('cd_agent b', 'a.aid = b.id')
                ->field('a.id,a.aid,a.create_time,a.update_time,b.name,b.mobile,a.order_num,a.num,a.is_delivery')
                ->where($searchData)
                ->order('is_delivery')
                ->limit(30)
                ->select();

            if(count($list) == 0) {
                throw new LoginException([
                    'msg' => '没有找到数据'
                ]);
            }else {
                throw new SuccessMessage([
                    'data' => $list
                ]);
            }
        }else {
            $delivery = Request::instance()->param('delivery');
            $condition = array(
                'a.status' => 1,
            );
            $condition['is_delivery'] = 0;

            $list = AgentOrder::name('agent_order')->alias('a')
                ->join('cd_agent b', 'a.aid = b.id')
                ->field('a.id,a.aid,a.create_time,a.update_time,b.name,b.mobile,a.order_num,a.num,a.is_delivery')
                ->where($condition)
                ->order('is_delivery')
                ->paginate(20);
        }

        $this->assign('list', $list);
        return view();
    }

}
